package cn.tianyigps.tianyigpsynclean.hanlder;

import cn.tianyigps.tianyigpspublic.param.bo.Task;
import cn.tianyigps.tianyigpskafka.task.manger.NewTaskManager;
import cn.tianyigps.tianyigpspublic.param.constant.CollectionNameConstant;
import cn.tianyigps.tianyigpspublic.param.constant.EquipmentReceiveConstant;
import cn.tianyigps.tianyigpspublic.util.FormatUtil;
import cn.tianyigps.tianyigpspublic.bean.EquipmentLastData;
import cn.tianyigps.tianyigpspublic.param.bo.EquipmentParamBO;
import cn.tianyigps.tianyigpsynclean.dao.DataCleanDao;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: WEI.YUAN
 * @Descirption:
 * @Date: 2019/4/25_14:33
 */
@Component
@Slf4j
public class DataCheckHandler {
    private static ObjectMapper OM = new ObjectMapper();
    @Autowired
    DataCleanDao dataCleanDao;

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    HandOutTaskHandler handOutTask;

    /**
     * 参数校验是否改变
     *
     * @param equipmentLastData
     * @param task
     */
    public void checkData(EquipmentLastData equipmentLastData, Task task) {
        //新上传轨迹数据
        Map<String, String> newMap = task.getParams();
        //旧轨迹数据
        Map<String, EquipmentParamBO> oldMap = equipmentLastData.getParams();
        Map<String, Object> oldLongLat = equipmentLastData.getMaps();

        //新增轨迹数据
        EquipmentLastData upEqLastData = new EquipmentLastData();
        Map<String, EquipmentParamBO> upMap = equipmentLastData.getParams();
        Map<String, Object> upLongLat = equipmentLastData.getMaps();

        //下发改变轨迹数据
        EquipmentLastData handLastData = new EquipmentLastData();
        Map<String, EquipmentParamBO> handMap = new HashMap<>();
        Map<String, Object> handLongLat = new HashMap<>();


        newMap.forEach((status, value) -> {
            EquipmentParamBO equipmentParamBO = new EquipmentParamBO();
            if (status.equals(EquipmentReceiveConstant.Latitude) || status.equals(EquipmentReceiveConstant.Longitude)) {
                //当经纬度发生改变时
                if (!value.equals(oldLongLat.get(status))) {
                    upLongLat.put(EquipmentReceiveConstant.Latitude, Double.valueOf(newMap.get(EquipmentReceiveConstant.Latitude)));
                    upLongLat.put(EquipmentReceiveConstant.Longitude, Double.valueOf(newMap.get(EquipmentReceiveConstant.Longitude)));
                    upLongLat.put("time", FormatUtil.stampToString(newMap.get(EquipmentReceiveConstant.UpTime)));

                    //添加下发改变轨迹数据
                    handLongLat.put(EquipmentReceiveConstant.Latitude, Double.valueOf(newMap.get(EquipmentReceiveConstant.Latitude)));
                    handLongLat.put(EquipmentReceiveConstant.Longitude, Double.valueOf(newMap.get(EquipmentReceiveConstant.Longitude)));
                    handLongLat.put(EquipmentReceiveConstant.Longitude, Double.valueOf(newMap.get(EquipmentReceiveConstant.Longitude)));
                }
            } else if (StringUtils.isEmpty(oldMap.get(status)) || !value.equals(oldMap.get(status).getValue())) {
                equipmentParamBO.setKey(status);
                equipmentParamBO.setValue(value);
                equipmentParamBO.setTime(FormatUtil.stampToString(newMap.get(EquipmentReceiveConstant.UpTime)));
                upMap.put(status, equipmentParamBO);
                //添加下发改变轨迹数据
                handMap.put(status, equipmentParamBO);
            }
        });

        if (!CollectionUtils.isEmpty(upMap)) {
            upEqLastData.setParams(upMap);
            handLastData.setParams(handMap);
        }
        if (!CollectionUtils.isEmpty(upLongLat)) {
            upEqLastData.setMaps(upLongLat);
            handLastData.setMaps(handLongLat);
        }
        if (upEqLastData != null) {
            //当upEqLastData不为空时则数据发生改变，更改数据表并下发任务
            upEqLastData.setSignalTime(FormatUtil.stampToDate(newMap.get(EquipmentReceiveConstant.UpTime)));
            upEqLastData.setEquipmentId(newMap.get(EquipmentReceiveConstant.DeviceCode));

            handLastData.setSignalTime(FormatUtil.stampToDate(newMap.get(EquipmentReceiveConstant.UpTime)));
            handLastData.setEquipmentId(newMap.get(EquipmentReceiveConstant.DeviceCode));
            //更新数据库同步刷新redis
            try {
                //同步redis
                redisTemplate.opsForValue().set(upEqLastData.getEquipmentId() + "lastlocation", upEqLastData);

                dataCleanDao.updateEquipmentLastData(handLastData, CollectionNameConstant.EQUIPMENT_LAST_DATA);
            } catch (Exception e) {
                e.printStackTrace();
                log.error("更新equipmentlastData数据失败:{}", e);
                return;
            }
            //分发任务
            handOutTask.handOutTask(handLastData);
        }

    }

}
